<%--
  Created by IntelliJ IDEA.
  User: 24207
  Date: 2020/6/11
  Time: 20:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!-- 引入jstl表达式 -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <meta charset="utf-8" />
    <title>用户登录</title>
    <!-- 引入JQuery-->
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <!--用百度的静态资源库的cdn安装bootstrap环境-->
    <!-- Bootstrap 核心 CSS 文件 -->
    <link href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet">
    <!--font-awesome 核心我CSS 文件-->
    <link href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet">
    <!-- 引入自定义css文件 -->
    <link href="static/css/main.css" rel="stylesheet" type="text/css">
    <!-- 在bootstrap.min.js 之前引入 -->
    <link rel='stylesheet' href='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css'>
    <!-- Bootstrap 核心 JavaScript 文件 -->
    <script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script>

    <!--引入jquery.validate-->
    <script src="http://static.runoob.com/assets/jquery-validation-1.14.0/lib/jquery.js"></script>
    <script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/jquery.validate.min.js"></script>
    <script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/localization/messages_zh.js"></script>
    <style type="text/css">
        body{
            background-image: linear-gradient(to right,#f2dede,#a6e1ec);
        }
    </style>
</head>
<body>
<!--
    基础知识：
    网格系统:通过行和列布局
    行必须放在container内
    手机用col-xs-*
    平板用col-sm-*
    笔记本或普通台式电脑用col-md-*
    大型设备台式电脑用col-lg-*
    为了兼容多个设备，可以用多个col-*-*来控制；
-->
<%-- 页面顶部留白 --%>
<div class="container">
    <div class="row" style="height: 5%"></div>
</div>
<div class="container" id="cot">
    <div class="row">
        <div class="col-md-offset-3 col-md-6">
            <form class="form-horizontal" id="login_form" action="login" method="post">
                <span class="heading">用户登录</span>

                <!-- 用户名文本框 -->
                <div class="form-group">
                    <input type="text" class="form-control" id="username" name="username" placeholder="用户名">
                    <i class="fa fa-user"></i>
                </div>
                <!-- 密码文本框 -->
                <div class="form-group help">
                    <input type="password" class="form-control" id="password" name="password" placeholder="密　码">
                    <i class="fa fa-lock"></i>
                </div>
                <!-- 错误信息 -->
                <label id="errormessage" for="login_form">用户名或密码错误</label>

                <div class="form-group" id="btn_row">
                    <!--  登录、注册按钮-->
                    <a href="turnToRegister" class="btn btn-default" id="register_btn">前往注册</a>
<%--                    <button type="submit" class="btn btn-default">登录</button>--%>
                    <input type="button" class="btn btn-default" value="登录" onclick="clickLogin()"/>
                </div>
            </form>
        </div>
    </div>
</div>

</body>
<script type="text/javascript">
    $().ready(function(){
        $("#errormessage").hide();
        $("#login_form").validate({
            rules: {
                username: {
                    required: true,
                    minlength: 5
                },
                password: {
                    required: true,
                    minlength: 5
                }
            },
            messages: {
                username: {
                    required: "请输入用户名",
                    minlength: "用户名至少由五位字符组成"
                },
                password: {
                    required: "请输入密码",
                    minlength: "密码至少由五位字符组成"
                }
            }
        });
    });

    function clickLogin(){
        var validator =$("#login_form").validate();
        if(validator.form()){
            $.post("login",
                {'username':$("#username").val(),'password':$("#password").val()},
                function(data){
                    if(data == "true") {
                        window.location.href="donateGoods";
                    } else{
                        console.log("The username doesn't match the password");
                        $("#errormessage").show();
                        setTimeout(function(){
                            $("#errormessage").hide();
                        },2000);
                    }
            })
        }
    }
</script>
</html>
